<?php

/**
* This is the model class for table "tbl_tag".
*
* The followings are the available columns in table 'tbl_tag':
* @property integer $id
* @property string $name
* @property integer $frequency
*/
class Tag extends CActiveRecord
{
    /**
    * Returns the static model of the specified AR class.
    * @return Tag the static model class
    */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
    * @return string the associated database table name
    */
    public function tableName()
    {
        return 'tbl_tag';
    }

    /**
    * @return array validation rules for model attributes.
    */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
        array('frequency', 'numerical', 'integerOnly'=>true),
        array('name', 'length', 'max'=>255),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, name, frequency', 'safe', 'on'=>'search'),
        );
    }

    /**
    * @return array relational rules.
    */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
    * @return array customized attribute labels (name=>label)
    */
    public function attributeLabels()
    {
        return array(
        'id' => 'ID',
        'name' => 'Name',
        'frequency' => 'Frequency',
        );
    }

    /**
    * Retrieves a list of models based on the current search/filter conditions.
    * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
    */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('frequency',$this->frequency);

        return new CActiveDataProvider(get_class($this), array(
        'criteria'=>$criteria,
        ));
    }


    public function array2string($a)
    {
        return implode(',',$a)  ;
    }
    public function string2array($s)
    {
        return explode(',',$s);
    }
    public function updateFrequency($old_tag, $new_tag)
    {
        $old_tag=$this->string2array($old_tag);
        $new_tag=$this->string2array($new_tag);
        $addtag=array_diff($new_tag,$old_tag);
        $removetag=array_diff($old_tag,$new_tag);   
        if(count($addtag)) 
            $this->AddTags($addtag);
        if(count($removetag))    
            $this->RemoveTags($removetag);
    }


    public function CheckExistTag($name)
    {
        //$tags=Tag::model()->find('name=:name AND id=:id',array(':name'=>$name,':id'=>$id));
        $tags=Tag::model()->find('name=:name',array(':name'=>$name));
        return $tags;
    }
    public function AddTags($tags)
    {
        foreach ($tags as $name) {
            if($name!=''){
                $temp=$this->CheckExistTag($name);
                if($temp==null)
                {
                    $tag=new Tag;
                    $tag->name= $name;
                    $tag->frequency=1;
                    $tag->save();
                }
                else{
                    $tag=Tag::model()->findByPk($temp->id);                
                    $tag->frequency=(int)$temp->frequency+1;
                    $tag->save();
                } 
            }

        }
    }
    public function RemoveTags($tags)
    {
        foreach ($tags as $name) {
            if($name!=''){
                $temp=$this->CheckExistTag($name);
                if($temp->frequency==1)
                {
                    $tag=Tag::model()->findByPk($temp->id);    
                    $tag->delete();
                }
                else{
                    $tag=Tag::model()->findByPk($temp->id);                
                    $tag->frequency=(int)$temp->frequency-1;
                    $tag->save();
                }
            }
        }
    }
    public function findTagWeights()
    {
        $criteria = new CDbCriteria();
        $criteria->order = 'frequency DESC';
        $criteria->limit = 20;
        $tags = Tag::model()->findAll($criteria);
        $tagWeigths = array();
        foreach($tags as $tag)
        {
            $weigth = $tag->frequency+8;
            $weigth = $weigth>=12 ? 12 : $weigth;
            $tagWeigths[$tag->name] = $weigth;
        }
        return $tagWeigths;
    }

}